from typing import List

MOD = 10 ** 9 + 7


class Solution:
    # 如果是偶数，则将会访问下一个房间，如果已经是最右侧则访问0号房间
    # 第1次访问会直接跳转，第2个访问会访问下一间
    # 只会向前跳转，不会向后跳转
    def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
        n = len(nextVisit)

        # dp[i] = 第1次到达i房间的天数
        dp = [0] * n
        for i in range(n - 1):
            nxt = nextVisit[i]
            if nxt == i:  # 指向当前房间
                dp[i + 1] = (dp[i] + 2) % MOD
            else:  # 指向之前的房间
                dp[i + 1] = (dp[i] + (dp[i] - dp[nxt]) + 2) % MOD
            # print(dp)

        return dp[-1]


if __name__ == "__main__":
    print(Solution().firstDayBeenInAllRooms(nextVisit=[0, 0]))  # 2
    print(Solution().firstDayBeenInAllRooms(nextVisit=[0, 0, 2]))  # 6
    print(Solution().firstDayBeenInAllRooms(nextVisit=[0, 1, 2, 0]))  # 6
